package com.sunwayworld.basemodule.samplemanage.samplecodegenerate.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.basemodule.samplemanage.samplecodegenerate.bean.LimsSamplecodeGenerateBean;
import com.sunwayworld.basemodule.samplemanage.samplecodegenerate.bean.LimsSamplecodeGenerateRecordBean;
import com.sunwayworld.basemodule.samplemanage.samplecodegenerate.dao.LimsSamplecodeGenerateDao;
import com.sunwayworld.basemodule.samplemanage.samplecodegenerate.service.LimsSamplecodeGenerateRecordService;
import com.sunwayworld.basemodule.samplemanage.samplecodegenerate.service.LimsSamplecodeGenerateService;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsSamplecodeGenerateServiceImpl implements LimsSamplecodeGenerateService {

    @Autowired
    private LimsSamplecodeGenerateDao limsSamplecodeGenerateDao;

    @Autowired
    private LimsSamplecodeGenerateRecordService generateRecordService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsSamplecodeGenerateDao getDao() {
        return limsSamplecodeGenerateDao;
    }

    @Autowired
    private LimsSamplecodeGenerateRecordService limsSamplecodeGenerateRecordService;

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsSamplecodeGenerateBean limsSamplecodeGenerate = jsonWrapper.parseUnique(LimsSamplecodeGenerateBean.class);
        limsSamplecodeGenerate.setId(ApplicationContextHelper.getNextIdentity());
        limsSamplecodeGenerate.setGenerateBatchCode(ApplicationContextHelper.getNextSequence(getDao().getTable()));

        List<LimsSamplecodeGenerateBean> beans = new ArrayList<>();
        beans.add(limsSamplecodeGenerate);
        this.generateSampleCode(beans);

        getDao().insert(limsSamplecodeGenerate);
        return limsSamplecodeGenerate.getId();
    }

    public void generateSampleCode(List<LimsSamplecodeGenerateBean> generateBeans) {
        List<LimsSamplecodeGenerateRecordBean> insertBeans = new ArrayList<>();
        generateBeans.forEach(g -> {
            if (null != g.getBarcodeNum()) {
                List<Long> ids = ApplicationContextHelper.getNextIdentityList(g.getBarcodeNum().intValue());
                List<String> geneCodes = ApplicationContextHelper.getNextSequenceList(limsSamplecodeGenerateRecordService.getDao().getTable(), "S", g.getBarcodeNum().intValue());
                for (int i = 0; i < g.getBarcodeNum(); i++) {
                    LimsSamplecodeGenerateRecordBean insertBean = new LimsSamplecodeGenerateRecordBean();
                    insertBean.setId(ids.get(i));
                    insertBean.setGenerateId(g.getId());
                    insertBean.setGenerateSampleCode(geneCodes.get(i));
                    insertBean.setPrintedCount(0L);
                    insertBeans.add(insertBean);
                }
            }
        });
        limsSamplecodeGenerateRecordService.getDao().insert(insertBeans);
    }

    @Override
    public JSONObject generateSampleCode(RestJsonWrapperBean jsonWrapperBean) {
        JSONObject req = new JSONObject();

        List<LimsSamplecodeGenerateBean> samplecodeGenerateBeans = jsonWrapperBean.parse(LimsSamplecodeGenerateBean.class);
        this.generateSampleCode(samplecodeGenerateBeans);

        return req;
    }

    @Override
    @Transactional
    /*
     * 样本编码生成管理-删除
     * 级联删除样本编号生成器记录数据
     * */
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsSamplecodeGenerateBean> samplecodeGenerates = wrapper.parse(LimsSamplecodeGenerateBean.class);
        if (CollectionUtils.isEmpty(samplecodeGenerates)) {
            return;
        }
        getDao().deleteBy(samplecodeGenerates);
        // 获取IDS
        List<Long> generateIds = samplecodeGenerates.stream().map(LimsSamplecodeGenerateBean::getId).collect(Collectors.toList());
        // 查询生成记录数据
        List<LimsSamplecodeGenerateRecordBean> generateRecords = generateRecordService.selectListByFilter(SearchFilter.instance()
                .match("GENERATEID", generateIds).filter(MatchPattern.OR));
        // 删除样本编码生成记录
        if (!CollectionUtils.isEmpty(generateRecords)) {
            generateRecordService.getDao().deleteBy(generateRecords);
        }
    }

}
